<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html>
<head>
<title>Reading data with SqliteDataReader</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="PHP, PostgreSQL, database, tutorial, learn PostgreSQL PHP ">
<meta name="description" content="In this chapter of the PostgreSQL PHP tutorial, we read
 data from the database using.">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container2">

<div id="wide_ad" class="ltow">

<div>
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

</div>


<div class="content2">

<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>


<h1>Retrieving data</h1>

<p>
There are several functions to read data from a database.
The data can be fetched as an enumerated array, as an object or
as an associated array. 
</p>

<div class="big_hor">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-9706709751191532";
/* big_horizontal */
google_ad_slot = "2904953388";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<p>
There are three steps to retrieve data from a database. First we define
an SQL SELECT statement. The statement is executed with the <code>pg_query()</code>
function. (In case of prepared statements, we would use <code>pg_execute()</code> function.)
We receive a result set object. Using the result set, we fetch the data with
<code>pg_fetch_row()</code>, <code>pg_fetch_assoc()</code> or 
<code>pg_fetch_object()</code> functions.
</p>


<pre class="code">
&lt;?php 

$host = "localhost"; 
$user = "user12"; 
$pass = "34klq*"; 
$db = "testdb"; 

$con = pg_connect("host=$host dbname=$db user=$user password=$pass")
    or die ("Could not connect to server\n"); 

$query = "SELECT * FROM cars LIMIT 5"; 

$rs = pg_query($con, $query) or die("Cannot execute query: $query\n");

while ($row = pg_fetch_row($rs)) {
  echo "$row[0] $row[1] $row[2]\n";
}

pg_close($con); 

?&gt;
</pre>

<p>
We get 5 cars from the cars table and print them to 
the console. 
</p>

<pre class="explanation">
$query = "SELECT * FROM cars LIMIT 5";
</pre>

<p>
This is the SQL to fetch 5 rows of cars. 
</p>

<pre class="explanation">
$rs = pg_query($con, $query) or die("Cannot execute query: $query\n");
</pre>

<p>
The query is executed with the <code>pg_query()</code> function.
The function returns a result set. 
</p>

<pre class="explanation">
while ($row = pg_fetch_row($rs)) {
  echo "$row[0] $row[1] $row[2]\n";
}
</pre>

<p>
The <code>pg_fetch_row()</code> function returns an array of
string values. We can use array index notation to get the
array fields. When there are no more rows, the function returns
false and the while loop terminates.
</p>

<pre>
$ php retrieve1.php
1 Audi 52642
2 Mercedes 57127
3 Skoda 9000
4 Volvo 29000
5 Bentley 350000
</pre>

<p>
Example output.
</p>

<hr class="btm">

<p>
In the second example, we will fetch data with the 
<code>pg_fetch_assoc()</code> function. 
</p>

<pre class="code">
&lt;?php 

$host = "localhost"; 
$user = "user12"; 
$pass = "34klq*"; 
$db = "testdb"; 

$con = pg_connect("host=$host dbname=$db user=$user password=$pass")
    or die ("Could not connect to server\n"); 

$query = "SELECT * FROM cars LIMIT 5"; 

$rs = pg_query($con, $query) or die("Cannot execute query: $query\n");

while ($row = pg_fetch_assoc($rs)) {
    echo $row['id'] . " " . $row['name'] . " " . $row['price'];
    echo "\n";
}

pg_close($con);

?&gt;
</pre>

<p>
The <code>pg_fetch_assoc()</code> function fetches a row as an associative array.
The keys of the associative array are the column names.
</p>

<pre class="explanation">
while ($row = pg_fetch_assoc($rs)) {
    echo $row['id'] . " " . $row['name'] . " " . $row['price'];
    echo "\n";
}
</pre>

<p>
The 'id', 'name' and 'price' are the keys to the returned 
associative array.
</p>

<hr class="btm">

<p>
In the last example, we will fetch the data with the
<code>pg_fetch_object()</code> function. It returns an object with properties 
that correspond to the fetched row's field names.
</p>

<pre class="code">
&lt;?php 

$host = "localhost"; 
$user = "user12"; 
$pass = "34klq*"; 
$db = "testdb"; 

$con = pg_connect("host=$host dbname=$db user=$user password=$pass")
    or die("Could not connect to server\n"); 

$query = "SELECT * FROM cars LIMIT 5"; 

$rs = pg_query($con, $query) or die("Cannot execute query: $query\n");

while ($ro = pg_fetch_object($rs)) {
    echo $ro->id . " ";
    echo $ro->name . " ";
    echo $ro->price . " ";
    echo "\n";
}

pg_close($con); 

?&gt;
</pre>

<p>
We select five cars from the cars table.
</p>

<pre class="explanation">
while ($ro = pg_fetch_object($rs)) {
    echo $ro->id . " ";
    echo $ro->name . " ";
    echo $ro->price . " ";
    echo "\n";
}
</pre>

<p>
The column names are the object properties, which hold
the values. 
</p>

<p>
We have finished reading data using <code>pg_fetch_row()</code>, <code>pg_fetch_assoc()</code>
and <code>pg_fetch_object()</code> functions.
</p>

<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡ 
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified May 3, 2012  <span class="copyright">&copy; 2007 - 2013 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>

